import mongoose from "mongoose";

// 定义基础的专业层级 Schema
const majorLevelSchema = new mongoose.Schema(
  {
    name: String,
    code: String,
    value: Number,
  },
  { _id: false }
);

// 扩展二级专业 Schema
const level2Schema = new mongoose.Schema(
  {
    name: String,
    code: String,
    value: Number,
    parentCode: String,
  },
  { _id: false }
);

// 扩展三级专业 Schema
const level3Schema = new mongoose.Schema(
  {
    name: String,
    code: String,
    value: Number,
    parentCode: String,
    type: String,
  },
  { _id: false }
);

// 定义父级关系 Schema
const parentRelationSchema = new mongoose.Schema(
  {
    level1: String,
    level2: String,
    type: String,
  },
  { _id: false }
);

const statisticsSchema = new mongoose.Schema({
  type: {
    type: String,
    required: true,
    enum: ["school", "province"],
  },
  lastUpdated: {
    type: Date,
    default: Date.now,
  },
  data: {
    schoolCount: Number,
    majorCount: Number,
    provinceCount: Number,
    provinces: [String],
    uniqueMajors: [String],
    majorDistribution: [
      {
        name: String,
        value: Number,
      },
    ],
    majorHierarchy: {
      types: [majorLevelSchema],
      level1: [majorLevelSchema],
      level2: [level2Schema],
      level3: [level3Schema],
      relationships: {
        level1ToLevel2: {
          type: Map,
          of: [String],
        },
        level2ToLevel3: {
          type: Map,
          of: [String],
        },
        level3ToParents: {
          type: Map,
          of: parentRelationSchema,
        },
      },
    },
    provinceScores: {
      national: {
        GDP: Number,
        employment: Number,
        income: Number,
        livingCost: Number,
        education: Number,
      },
      provinces: [
        {
          name: String,
          scores: {
            GDP: Number,
            employment: Number,
            income: Number,
            livingCost: Number,
            education: Number,
          },
        },
      ],
    },
  },
});

export const Statistics = mongoose.model("Statistics", statisticsSchema);
